#include "mxsetup.h"
#include "symbol.h"

ENTRY(__start)

/* the start address of code (.text* placement), default 512KB size */
#if defined (MX_USE_MMU)
    #define CODE_ADDR 0xffff000000080000
#else
    #define CODE_ADDR 0x40080000
#endif
#define CODE_SIZE MXSETUP_CODEREGION_SIZE

/* the start address of RAM (.data* .bss* STACK HEAP placement), default 1M size */
#define RAM_ADDR CODE_ADDR + CODE_SIZE
#define RAM_SIZE MXSETUP_RAMREGION_SIZE

/**
 * FIXED: following information can append to the `.bss` section, but after `.bss` section, the value of counter `.`
 *        can't be the end of RAM_RW(0xffff000000100000 + 512K).
 *        i don't know how to force `.` to the end of RAM_RW(0xffff000000100000 + 512K).
 * see the `链接脚本计数器"."不顺延的问题` in `编译工具链`
 */

MEMORY
{
    RAM_CODE(rx)    : ORIGIN = CODE_ADDR, LENGTH = CODE_SIZE    /* qemu-virt code startup address is 0x40080000 */
    RAM_RW(rw)      : ORIGIN = RAM_ADDR, LENGTH = RAM_SIZE
}

SECTIONS
{
    .text :
    {
        KEEP(*(.text.entrypoint));
        *(.text)
        *(.text.*)
        *(.rodata)
        *(.rodata.*)
    } > RAM_CODE

    /* mxboardinit symbol array */
    symbol_section_build(mxboardinit, RAM_CODE)

    /* mxappinit symbol array */
    symbol_section_build(mxappinit, RAM_CODE)

    /* terminal symbol array */
    symbol_section_build(terminal, RAM_CODE)

    .data :
    {
        . = ALIGN(8);
        __data_start = .;
        *(.data)
        *(.data.*)
        __data_end = .;
    } > RAM_RW

    .bss :
    {
        . = ALIGN(8);
        __bss_start = .;
        *(.bss)
        *(.bss.*)
        . = ALIGN(8);
        __bss_end = .;
        . = ALIGN(8);
        __stack_limit = .;
    } > RAM_RW
}

/* the stack base address, it is the top of stack (descending) */
__stack_base = __stack_limit + MXSETUP_STACKREGION_SIZE;

/* the heap information, NEED align 4k because of the mmu's entry */
__heap_start = ALIGN(__stack_base, 4096);   // there is a little waste here between __stack_base and __heap_start
__heap_end = RAM_ADDR + RAM_SIZE;

#ifdef MX_USE_MMU
/* the svma (physical address) */
__svma_pa_start = 0x40000000;
__svma_pa_end = 0x40080000;
#endif
